package net.xdclass.base_project.controller;


import net.xdclass.base_project.domain.JsonData;
import net.xdclass.base_project.task.AsyncTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;

@RestController
@RequestMapping("/api/v1")
public class UserController {

	@Autowired
	private AsyncTask asyncTask;

	@GetMapping("async_task")
    public JsonData testTask() throws InterruptedException {
		long begin = System.currentTimeMillis();
//		asyncTask.task1();
//		asyncTask.task2();
//		asyncTask.task3();

		Future<String> task4 = asyncTask.task4();
		Future<String> task5 = asyncTask.task5();
		Future<String> task6 = asyncTask.task6();
		//写一个死循环，当三个都执行完再返回
		for (;;){
			if (task4.isDone()&&task5.isDone()&&task6.isDone()){
				break;
			}
		}

		long end   = System.currentTimeMillis();
		long total = end -begin;
		System.out.println("总耗时："+total);
    	return JsonData.buildSuccess(total);
	}
}
